<?php

/**
 * Created by JetBrains PhpStorm.
 * User: root
 * Date: 10/4/12
 * Time: 11:42 PM
 * To change this template use File | Settings | File Templates.
 */
class Router_Regular extends Router_Abstract implements Router_Interface
{

    public function processRoute()
    {

        $this->module = fRequest::get(Config::get('app.module_uri_segment'), 'string', FALSE) ? fRequest::get(Config::get('app.module_uri_segment'), 'string', FALSE) : $this->module;

        $this->controller = fRequest::get(Config::get('app.controller_uri_segment'), 'string', FALSE) ? fRequest::get(Config::get('app.controller_uri_segment'), 'string', FALSE) : $this->controller;

        $this->action = fRequest::get(Config::get('app.action_uri_segment'), 'string', FALSE) ? fRequest::get(Config::get('app.action_uri_segment'), 'string', FALSE) : $this->action;
        
        return $this;

    }

    public function getLink($module, $controller, $action, $params = array(), $retainOtherParams = FALSE)
    {
        $section = Util::appSection(FALSE);

        $return = Config::get('app.url') . '/' . $section . '?m=' . $module . '&c=' . $controller . '&a=' . $action;

        $currentUrl = Util::getUrl('page');

        $tempArray = explode('?', $currentUrl);

        $allGetString = count($tempArray) > 1 ? $tempArray[1] : '';

        if (!$retainOtherParams or (count($tempArray) == 1))
        {
            if (count($params) > 0)
            {
                $getArray = array();

                foreach ($params as $k => $v)
                {
                    $getArray[] = $k . '=' . $v;
                }

                $return .= '&' . implode('&', $getArray);
            }

            return $return;
        } else
        {
            $allGetStringArray = explode('&', $allGetString);

            $allGetArray = array();

            foreach ($allGetStringArray as $getString)
            {
                $tmp = explode('=', $getString);

                if ((count($tmp) == 2) and (trim($tmp[1]) != ''))
                {
                    $allGetArray[$tmp[0]] = $tmp[1];
                }
            }

            unset($allGetArray['m']);

            unset($allGetArray['c']);

            unset($allGetArray['a']);

            if (count($params) > 0)
            {
                $getArray = array();

                foreach ($params as $k => $v)
                {
                    $getArray[] = $k . '=' . $v;

                    unset($allGetArray[$k]);
                }

                $return .= '&' . implode('&', $getArray);
            }

            if (count($allGetArray) > 0)
            {
                $getArray = array();

                foreach ($allGetArray as $k => $v)
                {
                    $getArray[] = $k . '=' . $v;
                }

                $return .= '&' . implode('&', $getArray);
            }

            return $return;
        }

    }

}